package com.cql.sys.handler;

import com.cql.pojo.Detials;
import com.github.pagehelper.PageInfo;
import com.cql.pojo.Dict;
import com.cql.sys.service.DictService;
import com.cql.utils.CommonResult;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @Description 字典相关控制层
 * @Date 2025/6/17 11:41
 * @Created by 12336
 */

@RestController
@RequestMapping("/dict")
public class DictController {

    @Resource
    private DictService dictService;

    @PostMapping("/add")
    public CommonResult addDict(@RequestBody Dict dict){

        System.out.println( "@@@"+dict);
        try {
            int result = dictService.addDict(dict);
            if(result > 0){
                return CommonResult.success(result);
            }
            if (result == 0){
                return CommonResult.error(500, "添加失败");
            }
            return CommonResult.error(-1, "重复添加");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.error(503, "系统异常");
        }
    }

    /**
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/find/{pageNum}/{pageSize}")
    public CommonResult<PageInfo<Dict>> findDictByPage(@PathVariable Integer pageNum, @PathVariable Integer pageSize){
        PageInfo<Dict> pageInfo = dictService.findDictByPage(pageNum, pageSize);
         return CommonResult.success(pageInfo);
    }
    @GetMapping("/find/{id}")
    public  CommonResult<Dict> findDictById(@PathVariable("id") Integer id){
        Dict dict = null;
        try {
            dict = dictService.findDictById(id);

            if (null == dict){
                return CommonResult.error(500, "未找到该字典");
            }
            return CommonResult.success(dict);
        } catch (Exception e) {
           return CommonResult.error(503, "系统异常,请联系管理员");
        }
    }

    @PostMapping("/update")
    public CommonResult updateDictById(@RequestBody Dict dict){
        try {
            System.out.println(dict);
            int result = dictService.updateDict(dict);
            System.out.println("result: " + result);
            return result > 0? CommonResult.success(result): CommonResult.error(500, "修改失败");
        }catch (Exception e){
            return CommonResult.error(503, "系统异常,请联系管理员");
        }
    }

    @DeleteMapping("/delete/{id}")
    public CommonResult deleteDictById(@PathVariable("id") Integer id){
        try {
            int result = dictService.deleteDictById(id);
            System.out.println("result:  "+ result);
            return result > 0? CommonResult.success(result): CommonResult.error(500, "删除失败");
        }catch (Exception e){
            return CommonResult.error(503, "系统异常,请联系管理员");
        }
    }

    @PostMapping("/findAllDetails}")
    public CommonResult<List<Detials>> findAllDetails(@RequestBody Dict dict){

        try {
            List<Detials> details = dictService.findAllDetails(dict);
            if (null == details){
                return CommonResult.error(500, "未找到该字典");
            }

            return CommonResult.success(details);
        } catch (Exception e) {
            return CommonResult.error(503, "系统异常,请联系管理员");
        }
    }


}
